//
//  AddCouponHeaderView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/7.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "AddCouponHeaderView.h"
@interface AddCouponHeaderView()<UITextFieldDelegate>


@end
@implementation AddCouponHeaderView

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        UIView * inPutContainerView = [[UIView alloc] initWithFrame:CGRectMake(globalMargin, globalMargin, ScreenWidth-2*globalMargin, Ratio_AddCoupon_Height(131))];
        inPutContainerView.tag = 100;
        inPutContainerView.backgroundColor = [UIColor clearColor];
        [self addSubview:inPutContainerView];
        
        UITextField * couponTextField = [[UITextField alloc] initWithFrame:CGRectMake(Ratio_AddCoupon_Width(10), Ratio_AddCoupon_Width(10), inPutContainerView.width - Ratio_AddCoupon_Width(20), Ratio_AddCoupon_Height(51))];
        [couponTextField addRadius:10];
        couponTextField.layer.borderWidth = 0.5;
        couponTextField.textAlignment = NSTextAlignmentCenter;
        couponTextField.clearButtonMode = UITextFieldViewModeAlways;
        couponTextField.clearsOnBeginEditing = YES;
        couponTextField.font = [UIFont systemFontOfSize:15];
        couponTextField.layer.borderColor = HexColor(@"#999999").CGColor;
        [inPutContainerView addSubview:couponTextField];
        couponTextField.placeholder = @"输入优惠码";
        couponTextField.delegate = self;
        self.textField = couponTextField;
        
        UIButton * applyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        applyButton.backgroundColor = HexColor(@"#3CB034");
        [applyButton setTitle:@"应用" forState:UIControlStateNormal];
        [applyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [applyButton addRadius:10];
        applyButton.width = couponTextField.width;
        applyButton.height = couponTextField.height;
        applyButton.x = couponTextField.x;
        applyButton.y = couponTextField.botton + Ratio_AddCoupon_Height(10);
        [applyButton addTarget:self action:@selector(applyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [inPutContainerView addSubview:applyButton];
    }
    return self;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)applyButtonClick:(UIButton*)aButton{
//    UIView * containerView = [self viewWithTag:100];
//    UIImageView * imgv = [[UIImageView alloc] initWithFrame:containerView.bounds];
//    imgv.image = [UIImage imageNamed:@"default_usedcoupon"];
//    [containerView addSubview:imgv];
//    imgv.userInteractionEnabled = YES;
//    [imgv addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(usedCouponClick:)]];
    
    [[LXDataManager manager] getCouponWithKeyWord:self.textField.text complete:^(BOOL success, id res) {
        if(success){
            [self alertWithTitle:@"" mesege:res[@"data"][@"error"] buttons:@[@"😯"] handle:^(NSInteger buttonIndex) {
                
            }];
        }
    }];
}

-(void)usedCouponClick:(UITapGestureRecognizer*)gesture{
    [gesture.view removeFromSuperview];
}
@end
